/**
 *
 */
package com.ssyt.examinationsystem.dao.impl;

import com.jdbc.util.JdbcUtils;
import com.ssyt.examinationsystem.dao.IScoreInfoDAO;
import com.ssyt.examinationsystem.entity.ScoreInfo;
import com.ssyt.examinationsystem.util.CollectionUtils;

import java.util.List;
import java.util.Map;

/**
 * @author Administrator
 */
public class ScoreInfoDAOImpl implements IScoreInfoDAO {

    @Override
    public int addScore(ScoreInfo score) {
        String sql = "insert into scoreinfo values(null,?,?,?,?)";
        Object[] parameters = {score.getPaper().getPaperId(), score.getStu().getStuId(), score.getObjectiveScore(),
                score.getSubjectiveScore()};
        return JdbcUtils.executeUpdate(sql, parameters);
    }

    @Override
    public int updateScoreByPaperIdAndStuId(ScoreInfo score) {
        String sql = "update scoreinfo set subjectiveScore=? where paperid=? and stuid=?";
        Object[] parameters = {score.getSubjectiveScore(), score.getPaper().getPaperId(), score.getStu().getStuId()};

        return JdbcUtils.executeUpdate(sql, parameters);
    }

    @Override
    public int updateScore(ScoreInfo score) {
        String sql = "update scoreinfo set paperid=?,stuid=?,objectiveScore=?,subjectiveScore=? where scoreid=?";
        Object[] parameters = {score.getPaper().getPaperId(), score.getStu().getStuId(), score.getObjectiveScore(),
                score.getSubjectiveScore(), score.getScoreId()};

        return JdbcUtils.executeUpdate(sql, parameters);
    }

    @Override
    public int deleteScore(String scoreId) {
        return JdbcUtils.executeUpdate("delete from scoreinfo where scoreid=?", scoreId);
    }

    @Override
    public List<Map<String, Object>> getScores() {
        return JdbcUtils.executeQuery("select * from scoreinfo");
    }

    @Override
    public Map<String, Object> getScoreByStuId(String stuId) {
        List<Map<String, Object>> list = JdbcUtils.executeQuery("select * from scoreinfo where stuid=?", stuId);
        return CollectionUtils.isEmptyForList(list) ? list.get(0) : null;
    }

}
